#pragma once

#include "TimeExpression.h"

#include "Observation.h"
#include "ValueSpecification.h"

namespace uml {

TimeExpression::TimeExpression(const QString& umlType)
    : ValueSpecification(umlType) {}

void TimeExpression::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    ValueSpecification::fromJson(json, errorList);
    expr_ = JsonSerializable::create<ValueSpecification>(json.value("expr"), errorList);

    JsonSerializable::fromJsonArray(observations_, "observations", json, errorList);
}

void TimeExpression::toJson(QJsonObject& json) {
    ValueSpecification::toJson(json);
    if (expr_) {
        QJsonObject expr_json;
        expr_->toJson(expr_json);
        json.insert("expr", expr_json);
    }
    JsonSerializable::toJsonArray(observations_, "observations", json);
}

/// <summary>
/// expr getter
/// A ValueSpecification that evaluates to the value of the TimeExpression.
/// </summary>
shared_ptr<ValueSpecification> TimeExpression::expr() const {
    return expr_;
}

/// <summary>
/// expr setter
/// A ValueSpecification that evaluates to the value of the TimeExpression.
/// </summary>
void TimeExpression::setExpr(shared_ptr<ValueSpecification> value) {
    expr_ = value;
}

/// <summary>
/// observations getter
/// Refers to the Observations that are involved in the computation of the
/// TimeExpression value.
/// </summary>
vector<shared_ptr<Observation>>& TimeExpression::observations() {
    return observations_;
}

} // namespace uml
